 /* This program estimates the event study models presented in Hoen, Brunner, and Schwegman, "Uplifting Winds: The Surprisingly Positive Community-Wide Impact of Wind Energy Installations on Property Values." The event study models use the estimator proposed by Sun and Abraham (2021).  */
 
 
log using results1, replace

	global Data "R:\ejb03002\District_Capitalization\Data"
	global Results "R:\ejb03002\District_Capitalization\Results"
	global Index "R:\ejb03002\District_Capitalization\Data\Housing Index"
	
	

	
* This is the main dataset
use "$Data\district_wind_dataset"



* Merge in house price index
* merge 1:1 ncesid year using "$Index\school_year_collapse_updated"                  /* All observations no donut hole */
* merge 1:1 ncesid year using "$Index\school_year_collapse_over1_half_updated"     /* 1 1/2 mile donut hole */
merge 1:1 ncesid year using "$Index\school_year_collapse_over2_updated"          /* 2 mile donut hole */
* merge 1:1 ncesid year using "$Index\school_year_collapse_within2_setp25"         /* Only within 2 miles plust control districts */
drop if _merge~=3
drop _merge

su ncesid min_p_year 


*********************************************

* Create housing characteristics based on first year of data
* Find Enrollment at First Turbine Installation
	drop mobs 
	sort ncesid year
	by ncesid: gen obs=_n if livingsquarefeetallbuildings2~=.
	egen mobs=min(obs), by(ncesid)
	gen temp2=livingsquarefeetallbuildings2 if obs==mobs
	egen livarea=max(temp2), by(ncesid)
	drop mobs temp* obs
	
	sort ncesid year
	by ncesid: gen obs=_n if fullbathsallbuildings2~=.
	egen mobs=min(obs), by(ncesid)
	gen temp2=fullbathsallbuildings2 if obs==mobs
	egen baths=max(temp2), by(ncesid)
	drop mobs temp* obs
	
	sort ncesid year
	by ncesid: gen obs=_n if yearbuilt2~=.
	egen mobs=min(obs), by(ncesid)
	gen temp2=yearbuilt2 if obs==mobs
	egen ybuilt=max(temp2), by(ncesid)
	drop mobs temp* obs
	
	
* Create Control Variables
gen trend=year-2004

gen livarea_trend=livarea*trend
gen bath_trend=baths*trend
gen ybuilt_trend=ybuilt*trend

gen inc_trend=medhhinc_00*trend
gen col_trend=pcoll_00*trend
gen enrl_trend=enrl_fy*trend
gen elem_trend=elem*trend
gen pop55_trend=pop55_00*trend
gen nwhite=1-white_00
gen nwhite_trend=nwhite*trend
gen density_trend=pop_dens00*trend
gen rural_trend=rural*trend

gen cap_pp=cum_p_cap / denrl

drop if inc_trend==.
drop if col_trend==.



******************************************************

* Create indicator for states with no turbines
gen state_no=0
	replace state_no=1 if fipst==1|fipst==5|fipst==11|fipst==12|fipst==13 ///
	|fipst==21|fipst==22|fipst==28|fipst==47|fipst==45|fipst==51

	
/* Sample Restrictions: Make base sample districts with a minimum enrollment of 
50 students and drop states where there are no turbines since they do not contribute to identification */



egen menrl=min(denrl), by(ncesid)
drop if menrl<50
drop if state_no==1

egen county_year=group(ccode year)

*********************************************************


	* Create Event Study Indicators
	
	* Create Event Study
	gen yrs_since=year-min_p_year
	gen treat1=1 if yrs_since~=.
    recode treat1 .=0
    replace yrs_since=0 if yrs_since==.

	gen tm6=1 if yrs_since<=-6 & yrs_since~=.
	recode tm6 .=0
	gen tm5=1 if yrs_since==-5
	recode tm5 .=0
	gen tm4=1 if yrs_since==-4
	recode tm4 .=0
	gen tm3=1 if yrs_since==-3
	recode tm3 .=0
	gen tm2=1 if yrs_since==-2
	recode tm2 .=0
	gen tm1=1 if yrs_since==-1
	recode tm1 .=0
	gen tp0=1 if yrs_since==0
	recode tp0 .=0
	gen tp1=1 if yrs_since==1
	recode tp1 .=0
	gen tp2=1 if yrs_since==2
	recode tp2 .=0
	gen tp3=1 if yrs_since==3
	recode tp3 .=0
	gen tp4=1 if yrs_since==4
	recode tp4 .=0
	gen tp5=1 if yrs_since==5
	recode tp5 .=0
	gen tp6=1 if yrs_since==6
	recode tp6 .=0
	gen tp7=1 if yrs_since==7
	recode tp7 .=0
	gen tp8=1 if  yrs_since==8
	recode tp8 .=0
	gen tp9=1 if yrs_since==9
	recode tp9 .=0
	gen tp10=1 if yrs_since==10
	recode tp10 .=0
	gen tp11=1 if yrs_since>=11  & yrs_since~=.
	recode tp11 .=0
	
	
* Sample Restrictions

 * Make temporal window 5 years prior to turbine operation and up to 10 years after
	drop if tm6==1
	drop if tp11==1

* Drop small number of districts with a p_year but no capacity

egen maxcap=max(cum_p_cap), by(ncesid)
drop if maxcap==0 & min_p_year~=.
drop maxcap
	
	
	
	
***********************************************************************

	gen treat=0
	replace treat=1 if year>=min_p_year & min_p_year~=.
	gen rprice=adj_sale_price*deflator
	gen lprice=log(rprice)
	

	egen maxcap=max(cap_pp), by(ncesid)
	egen temp=min(cap_pp) if cap_pp>0 , by(ncesid)
	replace temp=0 if temp==.
	egen mincap=max(temp), by(ncesid)
	drop temp
	
	local hcontrols livarea_trend bath_trend 
	local controls col_trend inc_trend pop55_trend nwhite_trend rural_trend
	replace cap_pp=mcap_pp /* Use baseline enrollment */

	replace cap_pp=mcap_pp /* Use baseline enrollment */
	
* Summary Statistics
replace cum_p_cap=. if cum_p_cap==0

su 	rprice denrl rlrev_pp rtrev_pp rtexp_pp rcexp_pp rtcapital_pp ptratio livarea baths pcoll_00 medhhinc_00 pop55_00 nwhite rural cum_p_cap cap_pp
su 	rprice denrl rlrev_pp rtrev_pp rtexp_pp rcexp_pp rtcapital_pp ptratio livarea baths pcoll_00 medhhinc_00 pop55_00 nwhite rural cum_p_cap cap_pp if min_p_year~=.
su 	rprice denrl rlrev_pp rtrev_pp rtexp_pp rcexp_pp rtcapital_pp ptratio livarea baths pcoll_00 medhhinc_00 pop55_00 nwhite rural cum_p_cap cap_pp if min_p_year==.


***********************************************************************************
* Create anual treatment indicators for event studies

global treat  tm5 tm4 tm3 tm2 tm1 tp1 tp2 tp3 tp4 tp5 tp6 tp7 tp8 tp9 tp10
global treat1 tm5 tm4 tm3 tm2 tp0 tp1 tp2 tp3 tp4 tp5 tp6 tp7 tp8 tp9 tp10

	label var tm5 "-5"
	label var tm4 "-4"
	label var tm3 "-3"
	label var tm2 "-2"
	label var tm1 "-1"
	label var tp0 "0"
	label var tp1 "1"
	label var tp2 "2"
	label var tp3 "3"
	label var tp4 "4"
	label var tp5 "5"
	label var tp6 "6"
	label var tp7 "7"
	label var tp8 "8"
	label var tp9 "9"
	label var tp10 "10"
	
	
	gen never_treat=0
	replace never_treat=1 if min_p_year==.
	

	
	eventstudyinteract lprice  $treat1  [w=transaction], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`hcontrols' `controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Log Housing Values) xtitle(Years Relative to Operation) ylabel(-.15(.05).15) ymtick(-.15(.05).15)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 
		
	graph export "$Results\hs1.tif", replace	


	
	eventstudyinteract  rlrev_pp $treat [w=denrl], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Real Local Reveneue Per-Pupil) xtitle(Years Relative to Operation) ylabel(-500(500)3000) ymtick(-500(500)3000)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	
	graph export "$Results\ES_lrev.tif", replace	

	
	eventstudyinteract  rlrev_pp $treat, cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Real Local Reveneue Per-Pupil) xtitle(Years Relative to Operation) ylabel(-500(500)3000) ymtick(-500(500)3000)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	graph export "$Results\ES_lrev_nc.tif", replace	

	

	*****************
	
	eventstudyinteract  rtrev_pp $treat [w=denrl], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Real Total Revenue Per-Pupil) xtitle(Years Relative to Operation) ylabel(-500(500)3000) ymtick(-500(500)3000)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	graph export "$Results\ES_trev.tif", replace	

	********************
	
	eventstudyinteract  rtexp_pp $treat [w=denrl], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Real Total Expenditures Per-Pupil) xtitle(Years Relative to Operation) ylabel(-500(500)3000)  ymtick(-500(500)3000)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	graph export "$Results\ES_texp.tif", replace	

	*************************
	
	eventstudyinteract  rcexp_pp $treat [w=denrl], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Real Current Expenditures Per-Pupil) xtitle(Years Relative to Operation) ylabel(-500(500)3000)  ymtick(-500(500)3000)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	graph export "$Results\ES_cexp.tif", replace	

	********************************
	
	eventstudyinteract  rtcapital_pp $treat [w=denrl], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Real Total Capital Expenditures Per-Pupil) xtitle(Years Relative to Operation) ylabel(-500(500)3000)  ymtick(-500(500)3000)  levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	graph export "$Results\ES_capital.tif", replace	

	
	**********************************************************************************************************
	
	eventstudyinteract  ptratio $treat [w=denrl], cohort(min_p_year) control_cohort(never_treat) absorb(ncesid county_year) vce(cluster ncesid)  covariates(`controls')

		matrix C = e(b_iw)
		mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
        matrix C = C \ A
        matrix list C
        coefplot matrix(C[1]), se(C[2]) vert ytitle(Pupil Teacher Ratio) xtitle(Years Relative to Operation) levels(90)  ///
		graphregion(color(white)) bgcolor(white) 

	graph export "$Results\ES_ptratio.tif", replace	

	
	
	